(function($, doc) {
	$.plusReady(function() {
		mui.init();
		var user = app.getState();
		var uid = user.user_id;
		var postpass = md5(user.password);
		plus.nativeUI.showWaiting("钱包同步中...");
		ethscan.getSysAmount(function(money, addr) {
			doc.getElementById('addr').innerHTML = addr;
			doc.getElementById('money').innerHTML = money;
			plus.nativeUI.closeWaiting();
		});
		/*
		var defCoin = ethscan.getDefCoin();
		var defAddr = defCoin['address'];
		var myWallet = ethscan.getWallet('eth');
		var provider = ethers.providers.getDefaultProvider('homestead');
		//console.log(user.eth_address);
		var wallet = new ethers.Wallet(myWallet.privkey, provider);
		var contracts = new ethers.Contract(defAddr, defCoin['abi'], wallet);
		//var contracts = new ethers.Contract(defAddr, defCoin['abi'], provider);
		doc.getElementById('addr').innerHTML = myWallet.address;
		plus.nativeUI.showWaiting("钱包同步中...");
		contracts.balanceOf(myWallet.address).then(function(res) {
			var etherString = ethers.utils.formatEther(res);
			//console.log(JSON.stringify(etherString));
			doc.getElementById('money').innerHTML = etherString;
			plus.nativeUI.closeWaiting();
		});*/
		var url = weburl + "/home/ChatApi/userinfo.html?u=" + uid + "&p=" + postpass;
		//console.log(url);
		mui.getJSON(url, function(res) {
			if(res.status == 1) {
				doc.getElementById("coin_balance").innerHTML = res.data.a1_money;
			}
		});
		var ii = 0;
		doc.getElementById('sub').addEventListener('tap', function() {
			ii++;
			if(ii > 1) {
				return;
			}
			var count = doc.getElementById("count").value;
			var password_account = doc.getElementById("password_account").value;
			var hasmoney = doc.getElementById('money').innerHTML * 1;
			if(count == "") {
				$.alert("请输入充币数量");
				return;
			}
			if(password_account == "") {
				$.alert("请输入交易密码");
				return;
			}
			if(hasmoney < 0.001) {
				$.alert("钱包余额不能低于0.001");
				return;
			}
			var url = weburl + "/home/ChatApi/userinfo.html?u=" + uid + "&p1=" + password_account;
			//plus.nativeUI.showWaiting("钱包校验中...");
			mui.getJSON(url, function(res) {
				if(res && res.status == 1) {
					ethscan.payToSystem(count, function(hash, addr) {
						if(!hash) {
							$.alert("支付失败！");
							return;
						}
						var url = weburl + "/home/ChatApi/chonglian.html";
						mui.post(url, {
							u: uid,
							p: postpass,
							c: count,
							p1: password_account,
							hash: hash,
							addr: doc.getElementById('addr').innerHTML,
							note: '充链备注'
						}, function(res) {
							$.alert(res.msg);
							plus.webview.currentWebview().close();
							return;
						});
					});
				} else {
					$.alert('交易密码错误！');
					//plus.nativeUI.closeWaiting();
					return;
				}
			});

			/*
			plus.nativeUI.showWaiting("钱包校验中...");
			var amount = ethers.utils.parseEther(count);
			contracts.balanceOf(user.eth_address).then(function(res) {
				var hasmoney = ethers.utils.formatEther(res);
				if(count > hasmoney) {
					plus.nativeUI.closeWaiting();
					$.alert("您的余额不足");
					return;
				}
				var options = {
					//gasPrice: 1100000000, // 1.1Gwei
					gasLimit: 250000
				};
				var callPromise = contracts.transfer(sysethaddr, amount, options);
				callPromise.then(function(obj) {
					console.log(JSON.stringify(obj));
					//$.toast('付款成功！');
					//dopost(obj.hash, toaddr);
					plus.nativeUI.closeWaiting();
					var url = weburl + "/home/ChatApi/chonglian.html";
					mui.post(url, {
						u: uid,
						p: postpass,
						c: count,
						p1: password_account,
						hash: obj.hash,
						note: '充链备注'
					}, function(res) {
						$.alert(res.msg);
						return;
					});

				})

				callPromise.catch(function(error) {
					console.log(JSON.stringify(error));
					plus.nativeUI.closeWaiting();
					$.toast('充链失败！');
					return;
				});

			});*/

		});
	});
}(mui, document));